package View;

import Model.Sach;
import dao.SachDAO;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

/**
 * @author TNT
 */
public class FormQuanLy extends JPanel {

   
    private MyTable table;
    private JSplitPane jSplitPane;
    private JScrollPane jScrollPane;
    private JPanel jPanel1, jPanel2;
    private AbstractForm form;
    private JLabel tieu_de_nhan, ma_sach_nhan, ten_sach_nhan, the_loai_nhan, tac_gia_nhan, nha_xuat_ban_nhan, ngay_xuat_ban_nhan, gia_ban_nhan, so_luong_nhan;
    private JTextField tieu_de_dai, ma_sach_dai, ten_sach_dai, the_loai_dai, tac_gia_dai, nha_xuat_ban_dai, ngay_xuat_ban_dai, gia_ban_dai, so_luong_dai;
    private JButton thong_tin, them, xoa, sua;

    public FormQuanLy(Sach[] sach) {
        jPanel1 = new JPanel();
        tieu_de_nhan = new JLabel("Quản Lý Sách");
        tieu_de_nhan.setFont(new Font(Font.SANS_SERIF, Font.ITALIC | Font.BOLD, 40));
        tieu_de_nhan.setForeground(new Color(0xCD3E2D));
        jPanel1.add(tieu_de_nhan);

        String[] ten_cot = {"Mã Sách", "Tên Sách", "Thể loại"};
//        Object[][] du_lieu = {{"T100", "Test 100", "Sci-Fi"}, {"T101", "Test 101", "Novel"}, {"T102", "Test 102", "asd"}};
        Object[][] du_lieu = new Object[sach.length][3];
        for (int i = 0; i < sach.length; i++) {
            Sach s = sach[i];
            du_lieu[i][0] = s.getMa_sach();
            du_lieu[i][1] = s.getTen_sach();
            du_lieu[i][2] = s.getMa_tl();
        }
        table = new MyTable(du_lieu, ten_cot);
        jScrollPane = new JScrollPane(table);
        jScrollPane.setPreferredSize(new Dimension(200, 280));
        jScrollPane.setMinimumSize(new Dimension(200, 280));
        jScrollPane.setMaximumSize(new Dimension(200, 280));

        form = new AbstractForm(5);
        form.addField("Tác Giả:", tac_gia_dai = new JTextField(16));
        form.addField("Nhà Xuất Bản:", nha_xuat_ban_dai = new JTextField(16));
        form.addField("Ngày Xuất Bản:", ngay_xuat_ban_dai = new JTextField(16));
        form.addField("Giá Bán:", gia_ban_dai = new JTextField(16));
        form.addField("Số Lượng:", so_luong_dai = new JTextField(16));

        ma_sach_nhan = new JLabel("Mã Sách:");
        ten_sach_nhan = new JLabel("Tên Sách:");
        the_loai_nhan = new JLabel("Thể Loại:");
        tac_gia_nhan = new JLabel("Tác Giả:");
        nha_xuat_ban_nhan = new JLabel("Nhà Xuất Bản:");
        ngay_xuat_ban_nhan = new JLabel("Ngày Xuất Bản:");
        gia_ban_nhan = new JLabel("Giá Bán:");
        so_luong_nhan = new JLabel("Số Lượng:");

        jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jScrollPane, form);
        jSplitPane.setDividerSize(1);
        jSplitPane.setEnabled(false);

        jPanel2 = new JPanel();
        thong_tin = new JButton("Thông Tin");
        them = new JButton("Thêm");
        xoa = new JButton("Xóa");
        sua = new JButton("Sửa");
        jPanel2.add(thong_tin);
        jPanel2.add(them);
        jPanel2.add(xoa);
        jPanel2.add(sua);

        setLayout(new BorderLayout());
        add(jPanel1, BorderLayout.NORTH);
        add(jSplitPane, BorderLayout.CENTER);
        add(jPanel2, BorderLayout.SOUTH);
    }

    private class MyTable extends JTable {

        public MyTable(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
            TableColumnModel columns = getColumnModel();

            setAutoCreateColumnsFromModel(false);
            setGridColor(Color.BLACK);
            setShowHorizontalLines(false);
            setBackground(new Color(0xF9F9F9));
            setSelectionBackground(new Color(0xFFFFCC));
            setRowHeight(32);
            setOpaque(false);
            getTableHeader().setReorderingAllowed(false);
            requestFocus();

            int[] sizes = new int[]{100, 200, 100};
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column) {
                    JLabel comp = (JLabel) super.getTableCellRendererComponent(
                            table, value, isSelected, hasFocus, row, column);
                    comp.setBorder(BorderFactory.createEmptyBorder(0, 7, 0, 0));
                    return comp;
                }
            };
            for (int i = 0; i < columnNames.length; i++) {
                columns.getColumn(i).setPreferredWidth(sizes[i]);
                columns.getColumn(i).setCellRenderer(renderer);
            }
        }
    }
}